package org.gameg.Api.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.catalina.User;
import org.gameg.Api.Service.ApiService;
import org.gameg.Api.entity.SysUserOperate;
import org.gameg.Api.util.ConfigProperty;
import org.gameg.Api.util.ResponseUtil;
import org.gameg.bean.Game;
import org.gameg.bean.Player;
import org.gameg.mapper.biz.TGameUserMapper;
import org.gameg.mapper.biz.TPlayerMapper;
import org.gameg.mapper.biz.TokenMapper;
import org.gameg.model.biz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.ibase4j.core.util.WebUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class APIController {

    @Autowired
    private ApiService apiService;

    @Autowired
    private TGameUserMapper tGameUserMapper;

    @Autowired
    private TPlayerMapper tPlayerMapper;

    /**
     * 通过得到的授权码信息，核对系统中的授权码，核对成功后，绑定用户并初始化密码
     * @return true 成功； false 失败（授权码错误）
     */
    @RequestMapping("/authorization")
    @ResponseBody
    public Object authorization(HttpServletRequest request){
        TPlayer player =  WebUtil.getParameter(request, TPlayer.class);
        Token token = WebUtil.getParameter(request, Token.class);
        Object isAuthorization = apiService.authorization(player,token);
        return isAuthorization;
    }

    @Autowired
    private TGame tGame;

    /**
     * 保存游戏设置
     * @param request
     * @return
     */
    @RequestMapping("/saveGame")
    @ResponseBody
    public Object saveGame(HttpServletRequest request,String jsonStr) throws IOException {
        //TGame tGame  = WebUtil.getParameter(request, TGame.class);
        boolean res = ConfigProperty.savePropertise(jsonStr);
        TGame tGame = new TGame();
        if (res) {
            tGame = this.tGame;
        }
        TGame param = WebUtil.getParameter(request, TGame.class);
        tGame.setPowers(param.getPowers());
        Map<String,Object> result = apiService.saveGame(tGame);
        return result;
    }


    /**
     * 保存积分：上分下分上庄下庄（积分设置）
     * @param request
     * @return
     */
    @RequestMapping("/saveIntegral")
    @ResponseBody
    public Map<String, Object> saveIntegral(HttpServletRequest request) {
        TPlayer player = WebUtil.getParameter(request,TPlayer.class);
        TGameUser gamePlayer = WebUtil.getParameter(request, TGameUser.class);
        Map<String, Object> result = apiService.saveIntegral(player, gamePlayer);
        return result;
    }

    /**
     * 获取游戏会员角色
     * @param request
     * @return
     */
    @RequestMapping("/getGameUser")
    @ResponseBody
    public Object getGameUser(HttpServletRequest request,String token) {
        Map<String, Object> map = new HashMap<>();
        TPlayer params = new TPlayer();
        params.setToken(token);
        TPlayer tPlayer = tPlayerMapper.selectOne(params);
        if (tPlayer == null) {
            map.put("msg", "用户已失效");
            map.put("code", 200);
            return map;
        } else {
            TGameUser gameUser = WebUtil.getParameter(request, TGameUser.class);
            TGameUser tGameUser = tGameUserMapper.selectOne(gameUser);
            return ResponseUtil.setSuccessModelMap(tGameUser);
        }

    }

    /**
     * 创建游戏会员角色
     * @param request
     * @return
     */
    @RequestMapping("/createGameUser")
    @ResponseBody
    public Map<String,Object> createGameUser(HttpServletRequest request,String token){
        TGameUser gameUser = WebUtil.getParameter(request, TGameUser.class);
        return apiService.createGameUser(gameUser,token);
    }

    /**
     * 获取游戏用户
     * @param request
     * @return
     */
    @RequestMapping("/getPlayer")
    @ResponseBody
    public Object getPlayer(HttpServletRequest request) {
        TPlayer tPlayer = WebUtil.getParameter(request, TPlayer.class);
        TPlayer player = tPlayerMapper.selectOne(tPlayer);
        if (player == null) {
            Map<String, Object> map = new HashMap<>();
            map.put("msg", "用户已过期");
            map.put("code", 200);
            map.put("data", null);
            return map;
        }
        return ResponseUtil.setSuccessModelMap(player);
    }


    @RequestMapping("/getTokenPage")
    @ResponseBody
    public Object getTokenPage(HttpServletRequest request,Integer pageSize,Integer pageNum,String playerTokens) {
        Page<Token> page = null;
        if (pageSize != null&&pageNum != null) {
            page = new Page<>(pageNum,pageSize);
        }
        if (page == null) {
            page = new Page<>();
        }
        Token token = WebUtil.getParameter(request, Token.class);
        if (playerTokens != null&&!playerTokens.equals("")) {
            token.setPlayerToken(playerTokens);
        }
        SysUser user = SysUserOperate.getUser();//  在session中获取的user
        if (user != null && !user.getUserName().equals("admin")) {//非admin用户，查询自己创建的授权码。
            token.setUserId(user.getId());
        }
        Page<Token> TokenPage = apiService.getTokenPage(page,token);
        return ResponseUtil.setSuccessModelMap(TokenPage);
    }

    @RequestMapping("/addOrUpDateToken")
    @ResponseBody
    public Object addOrUpDateToken(HttpServletRequest request){
        Token token = WebUtil.getParameter(request, Token.class);
        if (token == null) {
            token = new Token();
        }
        SysUser user = SysUserOperate.getUser();// 在session中获取的user
        token.setUserId(user.getId());
        return apiService.addOrUpDateToken(token);
    }

    @Autowired
    private TokenMapper tokenMapper;
    @RequestMapping("/deleteToken")
    @ResponseBody
    public Object deleteToken(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        Token token = WebUtil.getParameter(request, Token.class);
        if (SysUserOperate.getUser() != null && !SysUserOperate.getUser().getUserName().equals("admin")) {
            map.put("msg", "非超级管理员，禁止删除");
            map.put("code", 200);
            map.put("isSuccess", false);
            return map;
        }
        tokenMapper.delete(new EntityWrapper<Token>().where("id_={0}",token.getId()));
        map.put("msg", "删除成功");
        map.put("code", 200);
        map.put("isSuccess", true);
        return map;
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public Map<String,Object> getUser() {
        Map<String, Object> map = new HashMap<>();
        map.put("data", SysUserOperate.getUser());
        map.put("code", SysUserOperate.getUser()!=null?200:201);
        return map;
    }

    @RequestMapping("/getGamePlayStatistics")
    @ResponseBody
    public Object getGamePlayStatistics(HttpServletRequest request,Integer pageNum,Integer pageSize){
        TPlayer tPlayer = WebUtil.getParameter(request, TPlayer.class);
        Page<TGamePlayer> page = new Page<>(pageNum,pageSize);
        page = apiService.getGamePlayStatistics(page,tPlayer);
        return page;
    }
    @RequestMapping("/getGamePlayerDetail")
    @ResponseBody
    public Object getGamePlayerDetail(HttpServletRequest request, Long playerId ,Integer pageNum,Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Page<TGamePlayer> pageGame = new Page<>(pageNum,pageSize);
        Page<TOrder> pageOrder = new Page<>(pageNum,pageSize);
        map = apiService.getGamePlayerDetail(playerId,pageGame,pageOrder);
        return map;
    }
    @RequestMapping("/getOrderDetail")
    @ResponseBody
    public Object getOrderDetail(HttpServletRequest request, Long playerId ,Integer pageNum,Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Page<TOrder> pageOrder = new Page<>(pageNum,pageSize);
        map = apiService.getOrderDetail(playerId,pageOrder);
        return map;
    }
    @RequestMapping("/getDrawStatistics")
    @ResponseBody
    public Object getDrawStatistics(Long phone,String token) {
        Long playerId = phone;
        Map<String, Object> map = new HashMap<>();
        map = apiService.getDrawStatistics(playerId,token);
        return map;
    }


    /**
     * 设置奖池接口
     * @param request
     * @param token
     * @return
     */
    @RequestMapping("/addOrUpdateJackpot")
    @ResponseBody
    public Object addOrUpdateJackpot(HttpServletRequest request,String token) {
        TJackpot tJackpot = WebUtil.getParameter(request, TJackpot.class);
        return apiService.addOrUpdateJackpot(tJackpot, token);
    }

    /**
     * 获取奖池设置信息
     * @param request
     * @param token
     * @return
     */
    @RequestMapping("/getJackpot")
    @ResponseBody
    public Object getJackpot(HttpServletRequest request,String token) {
        TJackpot tJackpot = WebUtil.getParameter(request, TJackpot.class);
        return apiService.getJackpot(tJackpot, token);
    }


    @RequestMapping("/addOrUpdateCondition")
    @ResponseBody
    public Object addOrUpdateCondition(HttpServletRequest request,String token) {
        TCondition tCondition = WebUtil.getParameter(request, TCondition.class);
        return apiService.addOrUpdateCondition(tCondition, token);
    }

    /**
     * 获取奖励条件信息
     * @param request
     * @param token
     * @return
     */
    @RequestMapping("/getCondition")
    @ResponseBody
    public Object getCondition(HttpServletRequest request,String token) {
        TCondition condition = WebUtil.getParameter(request, TCondition.class);
        return apiService.getCondition(condition, token);
    }

    @RequestMapping("/addOrUpdateRebate")
    @ResponseBody
    public Object addOrUpdateRebate(HttpServletRequest request,String token) {
        TRebate rebate = WebUtil.getParameter(request, TRebate.class);
        return apiService.addOrUpdateRebate(rebate, token);
    }


    /**
     * 获取奖励条件信息
     * @param request
     * @param token
     * @return
     */
    @RequestMapping("/getCondition")
    @ResponseBody
    public Object getRebate(HttpServletRequest request,String token) {
        TRebate rebate = WebUtil.getParameter(request, TRebate.class);
        return apiService.getRebate(rebate, token);
    }




}
